import axios from 'axios'
const user = {
    state: {
        list: [],
        cacheUser: {}
    },
    mutations: {
        setUserData(state, lists){
            state.list = lists;
        },
        cacheUserData(state, list){
            state.cacheUser = list
        }
    },
    actions: {
        getUserData({commit}){
            axios.get('/people?transform=1')
            .then(res => {
                commit('setUserData',res.data.people);
            })
            .catch(err => {
                console.log(err)
            })
        },
        addUserData({commit,dispatch},obj){
            return axios.post('/people',obj)
            .then(res=>{
                return dispatch('updateBookType',obj).then(a=>{
                    if(a){
                        return res.data;
                    }
                });
                

            })
        },
        updateBookType({commit},obj){
            return axios.put('/bookmsg/'+JSON.parse(obj.booksName)[0].id,{type: '以借出'})
            .then(res => {
                if(res.data){
                    return res.data
                }
            })
        }

    }
}

export default user